package sz.szsmk.citizencard.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.NewsFragmentBinding;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonFragmentComponent;
import sz.szsmk.citizencard.model.network.bean.response.NoticeListRes;
import sz.szsmk.citizencard.model.network.bean.response.NoticeMainres;
import sz.szsmk.citizencard.mvp.contract.INewsContact;
import sz.szsmk.citizencard.mvp.presenter.NewsPresenter;
import sz.szsmk.citizencard.ui.adapter.NewsOrderAdapter;
import sz.szsmk.citizencard.ui.base.IBaseFragment;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.util.PubUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/6/4 17:04
 * description:
 */
@SuppressLint("ValidFragment")
public class NewsFragment extends IBaseFragment<NewsPresenter, NewsFragmentBinding>
        implements INewsContact.INewsView {
    private NewsOrderAdapter newsOrderAdapter;
    private SwipeRefreshLayout shopingSwiperefresh;
    private List<NoticeMainres.MainPageInfoListBean> infoList = new ArrayList<>();
    private List<String> zdList = new ArrayList<>();
    private List<String> htList = new ArrayList<>();
    private List<NoticeMainres.MainPageInfoListBean> overList = new ArrayList<>();
    private List<NoticeMainres.MainPageInfoListBean> showList;
    private List<NoticeMainres.MainPageInfoListBean> mList = new ArrayList<>();
    private NoticeMainres.MainPageInfoListBean bean;

    public static NewsFragment getInstance() {
        NewsFragment sf = new NewsFragment();
        return sf;
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.news_fragment;
    }

    @Override
    protected void initInjector() {
        DaggerCommonFragmentComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        zdList = new ArrayList<>();
        overList = new ArrayList<>();
        showList = new ArrayList<>();
        dataBinding.noticeSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    String word = dataBinding.noticeSearch.getText().toString();
                    if (TextUtils.isEmpty(word)) {
                        PubUtils.popTipOrWarn(getActivity(), "请输入搜索内容");
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putString("word", word);
                        bundle.putString("title", "搜索结果");
                        JumpUtils.jumptoNatvie(mContext, "noticelist", 0, bundle);
                    }
                    return true;
                }
                return false;
            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());

        dataBinding.newsRv.setLayoutManager(linearLayoutManager);

        dataBinding.swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getMainNotice();
            }
        });
        dataBinding.swipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getMainNotice();
            }
        });


        newsOrderAdapter = new NewsOrderAdapter(getActivity());
        newsOrderAdapter.setPopReleaseCallBack(new NewsOrderAdapter.ReleaseCallBack() {
            @Override
            public void onPopReleaseCallBack(String messageType) {
                mPresenter.changeIsRead(messageType, "1");
            }
        });
        newsOrderAdapter.setUpItemCallBack(new NewsOrderAdapter.UpitemCallBack() {
            @Override
            public void onUpItemCallBack(int position) {
            }
        });
        newsOrderAdapter.setDelItemCallBack(new NewsOrderAdapter.DelitemCallBack() {
            @Override
            public void onDelItemCallBack(int position) {
                NoticeMainres.MainPageInfoListBean mainPageInfoListBean = infoList.get(position);
                String messageType = mainPageInfoListBean.getMessageType();
                mPresenter.delItem(messageType);
                removeData(position);
            }
        });
        dataBinding.newsRv.setLayoutManager(new LinearLayoutManager(getContext()));
        dataBinding.newsRv.setAdapter(newsOrderAdapter);
        dataBinding.blurringView.setBlurredView(dataBinding.backView);
        dataBinding.blurringView.post(new Runnable() {
            @Override
            public void run() {
                dataBinding.blurringView.invalidateDataStatic();
            }
        });
    }


    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getMainNotice();
    }

    @Override
    protected void updateViews() {

    }

    public void removeData(int position) {
        infoList.remove(position);
//        删除动画
        newsOrderAdapter.notifyItemRemoved(position);
        newsOrderAdapter.notifyDataSetChanged();
    }

    @Override
    public void getMainNoticeError(String message) {

    }

    @Override
    public void getMainNoticeSucc(List<NoticeMainres.MainPageInfoListBean> mainPageInfoList) {
        infoList.clear();
        infoList.addAll(mainPageInfoList);
        newsOrderAdapter.setItemData(infoList);

    }


    @Override
    public void queryAndSearchSucc(List<NoticeListRes.MessageDetailListBean> detailList) {

    }

    @Override
    public void queryAndSearchFail(NoticeListRes response) {

    }

    @Override
    public void delItemSucc() {
        mPresenter.getMainNotice();
    }

    @Override
    public void delItemFail(String message) {

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        dataBinding.noticeSearch.clearFocus();
    }
}
